import Vue from 'vue'
import daohang from '@/components/daohang'
import blank from '@/components/blank'
import blank1 from '@/components/backend/blank1'
import home from '@/components/backend/home'
import cartoonindex from '@/components/backend/cartoonindex'
import wemedia from '@/components/backend/wemedia'
import user from '@/components/backend/user'
import userlevel from '@/components/backend/userlevel'
import person from '@/components/user/person'
import blank2 from '@/components/blank2'
import level from '@/components/level/level'
import login from '@/views/login/indexcopy'
import novel from '@/components/novel/novel'
import message from '@/components/chart/message'
import chart from '@/components/chart/chart'
import novelbook from '@/components/novel/novelbook'
import cartoon from '@/components/cartoon/cartoon'
import VueRouter from 'vue-router'
import novellist from '@/components/novel/novellist'
import orderindex from '@/components/order/orderindex'
import orderfooter from '@/components/order/orderfooter'
import orderget from '@/components/order/orderget'
import orderfa from '@/components/order/orderfa'
import orderput from '@/components/order/orderput'
import axios from 'axios'

Vue.use(VueRouter)



 const routes= [
  {
    path: '/orderindex',
    name: 'orderindex',
    component: orderindex
  },
  {
    path: '/orderfa',
    name: 'orderfa',
    component: orderfa
  },
  {
    path: '/orderput',
    name: 'orderput',
    component: orderput
  },
  {
    path: '/orderget',
    name: 'orderget',
    component: orderget
  },
  {
    path: '/orderfooter',
    name: 'orderfooter',
    component: orderfooter,
    children:[

    ]
  },
    {
      path: '/person',
      name: 'person',
      component: person
    },
    {
      path: '/blank',
      name: 'blank',
      component: blank
    },
    {
      path: '/chart',
      name: 'chart',
      component: chart
    },
    {
      path: '/message',
      name: 'message',
      component: message
    },
    {
      path: '/blank1',
      name: 'blank1',
      component: blank1
    },
    {
      path: '/blank2',
      name: 'blank2',
      component: blank2,
      redirect:"/home",
      children:[
        {path: '/user',
        name: 'user',
        component: user},
        {
          path: '/home',
        name: 'home',
        component: home
        },{path: '/userlevel',
        name: 'userlevel',
        component: userlevel},
        {path: '/cartoonindex',
        name: 'cartoonindex',
        component: cartoonindex},
        {path: '/wemedia',
        name: 'wemedia',
        component: wemedia},
      ]
    },

    {
      path: '/novelbook',
      name: 'novelbook',
      component: novelbook
    },
    {
      path: '/cartoon',
      name: 'cartoon',
      component: cartoon
    },
    {
      path: '/novellist',
      name: 'novellist',
      component: novellist
    },

    {
      path: '/novel',
      name: 'novel',
      component: novel
    },
    {
      path: '/',
      name: 'daohang',
      component: daohang
    },
    { // 定义组件的路径
      path: '/level',
      name: 'level',
      component: level,
      meta:{authRequired: true},
    },
    { // 定义组件的路径
      path: '/login',
      name: 'login',
      component: login,
      meta:{authRequired: true},
    }
  ]


const router=new VueRouter ({

  routes
});


router.beforeEach((to,from,next)=>{
  window.scrollTo(0,0);
  if(to.name==='daohang'||to.name==='login'){
    next()
  }else{
    let url='http://localhost:8098/iflogin'
    let param=`token=${localStorage.getItem('token')}`
    axios.post(url,param).then(resp=>{
      let result=resp.data;
      if(result.code===200){
        if(to.name==='blank2'||to.name==='wemedia'||to.name==='user'||to.name==='home'){
        if(result.data.identify==="超级管理"){
          next()
        }else{
          next({name:"daohang"})
        }
      }else{
        next()
      }
    }else if(result.code===0){
       next({name:"login"})
    }

    })
  }



})


export default router
